package com.hongxun.entity;

import android.content.Context;
import android.text.TextUtils;

import com.hongxun.budget.greendao.DPictureEntityDao;
import com.hongxun.budget.ui.activity.tk.SetMainActivity;
import com.hongxun.budget.utils.BitmapUtils;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.greenentity.tk.DPictureEntity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by admin on 2018/9/5.
 */

public class PhotoLabel {
    //标签
    private String label;
    //标签对应的内容
    private List<DPictureEntity> entities;
    private List<DPictureEntity> deleteEntities;
    private Context context;

    private PhotoLabel() {
    }

    public PhotoLabel(Context context, String label, String type) {
        if (type.equals("踏勘"))
            entities = GreenDaoManager.getInstance().getmDaoSession().getDPictureEntityDao().queryBuilder()
                    .where(DPictureEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                            , DPictureEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                            , DPictureEntityDao.Properties.PLabel.eq(label)
                            , DPictureEntityDao.Properties.PDatastate.notEq("删除")).list();
        else if (type.equals("验收"))
            entities = GreenDaoManager.getInstance().getmDaoSession().getDPictureEntityDao().queryBuilder()
                    .where(DPictureEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_ys_guid", ""))
                            , DPictureEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                            , DPictureEntityDao.Properties.PLabel.eq(label)
                            , DPictureEntityDao.Properties.PDatastate.notEq("删除")).list();
        this.label = label;
        this.context = context;
        entities.add(new DPictureEntity());//加一个空的站位符
        deleteEntities = new ArrayList<>();
    }

    public void save() {
        for (DPictureEntity d : deleteEntities) {
            d.setPDatastate("删除");
            if (GreenDaoManager.getInstance().getmDaoSession().getDPictureEntityDao().load(d.getGuid()) != null) {
                GreenDaoManager.getInstance().getmDaoSession().getDPictureEntityDao().update(d);
            }
            new File(d.getPAppaddress()).delete();
        }
        entities.remove(entities.size() - 1);//保存的时候去掉站位符
        for (DPictureEntity d : entities) {
            if (TextUtils.isEmpty(d.getGuid())) {
                d.setGuid(UUID.randomUUID().toString());
                d.setPGuid(SPUtils.get(context, "project_guid", "").toString());
                d.setPPGuid(SPUtils.get(context, "UUID", "").toString());
                d.setPLabel(label);
                d.setPDatastate("新增");
                d.setPPType(SPUtils.get(context, "point_name", "").toString());
                d.setPStatus("储备");
            }
        }
        GreenDaoManager.getInstance().getmDaoSession().getDPictureEntityDao().insertOrReplaceInTx(entities);
    }

    public void saveYs() {
        for (DPictureEntity d : deleteEntities) {
            d.setPDatastate("删除");
        }
        GreenDaoManager.getInstance().getmDaoSession().getDPictureEntityDao().updateInTx(deleteEntities);
        entities.remove(entities.size() - 1);//保存的时候去掉站位符
        for (DPictureEntity d : entities) {
            if (TextUtils.isEmpty(d.getGuid())) {
                d.setGuid(UUID.randomUUID().toString());
                d.setPGuid(SPUtils.get(context, "project_ys_guid", "").toString());
                d.setPPGuid(SPUtils.get(context, "UUID", "").toString());
                d.setPLabel(label);
                d.setPDatastate("新增");
                d.setPPType(SPUtils.get(context, "point_name", "").toString());
                d.setPStatus("验收");
            }
        }
        GreenDaoManager.getInstance().getmDaoSession().getDPictureEntityDao().insertOrReplaceInTx(entities);
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public List<DPictureEntity> getEntities() {
        return entities;
    }

    public void setEntities(List<DPictureEntity> entities) {
        this.entities = entities;
    }

    public List<DPictureEntity> getDeleteEntities() {
        return deleteEntities;
    }

    public void setDeleteEntities(List<DPictureEntity> deleteEntities) {
        this.deleteEntities = deleteEntities;
    }
}
